#!/bin/bash
# produced by 55159671@qq.com
wi=(7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2)
verifycode=(1 0 X 9 8 7 6 5 4 3 2)
S=0
str=$1
array=()
id18() {
    for ((i=0;i<17;i++)){ array[$i]=${str:$i:1}; }
    for ((i=0;i<17;i++)){ S=$((S+${array[i]}*${wi[i]})); }
    array[17]=${verifycode[$((S%11))]}
    echo -e "输出身份证号是：${str}${array[17]}"
}
(($#!=0)) && echo "输入身份证号是：$1"
case ${#1} in
    15)
        today=`date -I`
        shotyear=${1:6:2}
        ((${today:2:2} < ${1:6:2})) && bornyear=$((${today:0:2}-1))${1:6:2} || bornyear=${today:0:2}${1:6:2}
        str=${1:0:6}${bornyear}${1:8}
        id18;;
    17)
        id18;;
    *)
        echo "使用方法："
        echo "    ./verifycode.sh <15位一代身份证号码>"
        echo "或："
        echo "    ./verifycode.sh <17位二代身份证号码（不含校验码）>";;
esac
